package bitetianan_xing;

import bitetianan_xing.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class demo3_Insert {
    public static void main(String[] args) {
        Connection connection =null;
        PreparedStatement statement =null;
        //插入操作需不需要定义结果集对象？----不用，因为insert返回的是受影响的行数

        try {
            // 1.获取数据库连接
            connection = DBUtil.getConnection();
            //2.定义SQL
            String sql ="insert into student(sn,name,mail,class_id) values(?,?,?,?)";
            //3.定义预处理对象
            statement = connection.prepareStatement(sql);
            //4.接收用户信息
            System.out.println("请输入学号：");
            Scanner scanner = new Scanner(System.in);
            String sn = scanner.next();
            System.out.println("请输入姓名：");
            String name =scanner.next();
            System.out.println("请输入邮箱：");
            String mail =scanner.next();
            System.out.println("请输入班级编号：");
            Long classId =Long.valueOf(scanner.next());
            //5.用真实值替换占位符
            statement.setString(1,sn);
            statement.setString(2,name);
            statement.setString(3,mail);
            statement.setLong(4,classId);
            //6.执行SQL获取结果
            int row = statement.executeUpdate();
            //7.判断结果
            if (row==1){
                System.out.println("插入成功！");
            }else {
                System.out.println("插入失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //释放资源，关闭连接
            DBUtil.close(null,statement,connection);
        }
    }
}
